#!/usr/bin/python
#coding: utf-8

import sys
import os
import os.path
import subprocess
import traceback
import time

base='/host'

def umount(volume):
    try:
        subprocess.check_call('umount %s' % volume, shell=True)
    except:
        pass

def mount_points():
    with open('/proc/self/mountinfo', 'r') as f:
        mounts = f.read().decode('utf-8')
    points = set()
    for line in mounts.split('\n'):
        mtab = line.split()
        if len(mtab) > 1 and mtab[4].startswith(base + '/') and mtab[4].endswith('shm') and 'containers' in mtab[4]:
            points.add(mtab[4])
    return points

def cleanup():
    umounts = mount_points()
    volumes = [ v for v in umounts ]
    for volume in sorted(umounts, reverse=True):
        umount(volume)

def run():
    os.execve('/pilot/gdocker-fluent', ['/pilot/gdocker-fluent', '-template', '/pilot/fluentd.tpl','-base', base, '-log-level', 'debug' ], os.environ)

def config():
    subprocess.check_call(['/pilot/config.default'])

if __name__ == '__main__':
    config()
    cleanup()
    run()
